package com.boot.service.impl;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.dao.UserDao;
import com.boot.entity.R;
import com.boot.entity.Stats;
import com.boot.entity.User;
import com.boot.exception.ServiceException;
import com.boot.service.UserService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户表(User)表服务实现类
 *
 * @author makejava
 * @since 2023-07-20 18:48:26
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User queryUserByUsername(String username) {
        try {
            return this.query()
                    .eq("username", username)
                    .one();
        } catch (MybatisPlusException e) {
            return null;
        }
    }


    /**
     * 统计用户中男女的比例
     * @return
     */
    @Override
    public List<Stats> queryUserGender() {

        List<Stats> list = userDao.selectUserGender();
        return list;
    }

    @Override
    public boolean save(User entity) {
        User user = this.queryUserByUsername(entity.getUsername());
        if (user != null)
            throw new ServiceException(403, "用户已存在");
        return super.save(entity);
    }
}

